package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    //获取部门列表
    @GetMapping
    public Result GetAllDepts(){
        //调用service查询数据
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    };

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Log
    @DeleteMapping("/{id}")
    public Result DeleteById(@PathVariable Integer id){
        log.info("根据id删除部门{}", id);
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @Log
    @PostMapping
    public Result Insert(@RequestBody Dept dept){
        log.info("添加部门{}", dept.getName());
        deptService.insert(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getDeptById(@PathVariable Integer id){
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }

    /**
     * 根据id修改部门
     * @param dept
     * @return
     */
    @Log
    @PutMapping()
    public Result Update(@RequestBody Dept dept){
        log.info("部门{}", dept.getId());
        deptService.updateById(dept);
        return Result.success();
    }
}
